home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / bin / pycentral < prev    next >
Text File  |  2009-10-11  |  90KB  |  2,197 lines

  1. #! /usr/bin/python
  2.  
  3. import fnmatch, glob, os, re, string, sys, time, cStringIO
  4. from optparse import OptionParser
  5. from ConfigParser import SafeConfigParser
  6.  
  7. sys.path[0:0] = ['/usr/share/pycentral-data', '/usr/share/python']
  8. import pyversions
  9.  
  10. try:
  11.     SetType = set
  12. except NameError:
  13.     import sets
  14.     SetType = sets.Set
  15.     set = sets.Set
  16.  
  17. program = os.path.basename(sys.argv[0])
  18.  
  19. shared_base = '/usr/share/pycentral/'
  20. shared_base2 = '/usr/share/pyshared/'
  21. pycentral_version = '0.6.11ubuntu2'
  22. req_pycentral_version = '0.6.11'
  23.  
  24.  
  25. def get_file_overwrite_error(existing_files):
  26.     """
  27.     helper that returns a error string to be passed to the user when
  28.     local files are detected (and querries if they are local installs
  29.     or part of another package)
  30.     """
  31.     from subprocess import PIPE, Popen
  32.     l = []
  33.     # ask dpkg what it knows about the files in question
  34.     (stdout, stderr) = Popen(["dpkg","-S"]+existing_files,
  35.                              env={"LANG" : "C"},
  36.                              stdout=PIPE, stderr=PIPE).communicate()
  37.     # this should never happen, dpkg should always give us
  38.     # something but we deal with it for robustness
  39.     if not stdout and not stderr:
  40.         l.append("Error, can not overwrite existing files:")
  41.         l.extend(existing_files)
  42.     # stdout has files that belong to packages
  43.     if stdout:
  44.         l.append("Not overwriting files owned by other packages:")
  45.         for line in map(string.strip, stdout.split("\n")):
  46.              if line:
  47.                  l.append("  "+line)
  48.     # stderr has local (or maintainer script created) files
  49.     if stderr:
  50.         l.append("Not overwriting local files:")
  51.         for line in map(string.strip, stderr.split("\n")):
  52.              if line:
  53.                  l.append("   "+line)
  54.     return "\n".join(l)
  55.  
  56. def samefs(path1, path2):
  57.     if not (os.path.exists(path1) and os.path.exists(path2)):
  58.         return False
  59.     while path1 != os.path.dirname(path1):
  60.         if os.path.ismount(path1):
  61.             break
  62.         path1 = os.path.dirname(path1)
  63.     while path2 != os.path.dirname(path2):
  64.         if os.path.ismount(path2):
  65.             break
  66.         path2 = os.path.dirname(path2)
  67.     return path1 == path2
  68.  
  69. def version2depends(vinfo):
  70.     if isinstance(vinfo, set):
  71.         vinfo = list(vinfo)
  72.     if isinstance(vinfo, list):
  73.         vinfo = vinfo[:]
  74.         vinfo.sort()
  75.         nv = [int(s) for s in vinfo[-1].split('.')]
  76.         deps = 'python (>= %s), python (<< %d.%d)' % (vinfo[0], nv[0], nv[1]+1)
  77.     elif vinfo in ('all', 'current'):
  78.         supported = [d[6:] for d in pyversions.supported_versions()
  79.                      if re.match(r'python\d\.\d', d)]
  80.         supported.sort()
  81.         deps = 'python (>= %s)' % supported[0]
  82.     elif vinfo == 'current_ext':
  83.         cv = pyversions.default_version(version_only=True)
  84.         nv = [int(s) for s in cv.split('.')]
  85.         deps = 'python (>= %s), python (<< %d.%d)' % (cv, nv[0], nv[1]+1)
  86.     else:
  87.         raise ValueError, 'unknown version info %s' % vinfo
  88.     return deps + ', python-central (>= %s)' % req_pycentral_version
  89.         
  90. def third_party_dir(version):
  91.     if version.startswith('python'):
  92.         version = version[6:]
  93.     if version in ('2.3', '2.4', '2.5'):
  94.         return 'usr/lib/python' + version + '/site-packages'
  95.     else:
  96.         return 'usr/lib/python' + version + '/dist-packages'
  97.  
  98. def build_relative_link(tgt, link):
  99.     t = tgt.split('/')
  100.     l = link.split('/')
  101.     while l[0] == t[0]:
  102.         del l[0], t[0]
  103.     return '/'.join(['..' for i in range(len(l)-1)] + t)
  104.  
  105. def read_dpkg_status(verbose=False):
  106.         """Read the dpkg status file, return a list of packages depending
  107.         on python-central and having a Python-Version information field."""
  108.         packages = []
  109.         rx = re.compile(r'\bpython-central\b')
  110.         pkgname = version = None
  111.         depends = ''
  112.         status = []
  113.         for line in file('/var/lib/dpkg/status'):
  114.             if line.startswith('Package:'):
  115.                 if version != None and 'installed' in status:
  116.                     if 'python-support' in depends:
  117.                         pass
  118.                     elif rx.search(depends):
  119.                         packages.append((pkgname, version))
  120.                         if verbose:
  121.                             print "    %s: %s (%s)" % (pkgname, version, status)
  122.                 version = None
  123.                 status = []
  124.                 pkgname = line.split(':', 1)[1].strip()
  125.             elif line.startswith('Python-Version:'):
  126.                 version = line.split(':', 1)[1].strip()
  127.             elif line.startswith('Depends:'):
  128.                 depends = line.split(':', 1)[1].strip()
  129.             elif line.startswith('Status:'):
  130.                 status = line.split(':', 1)[1].strip().split()
  131.         if version != None and 'installed' in status:
  132.             if rx.search(depends):
  133.                 packages.append((pkgname, version))
  134.                 if verbose:
  135.                     print "    %s: %s (%s)" % (pkgname, version, status)
  136.         return packages
  137.  
  138.  
  139. class PyCentralConfigParser(SafeConfigParser):
  140.     '''SafeConfigParser allowing mixed case, `:' and `=' in keys'''
  141.     OPTCRE = re.compile(
  142.         r'(?P<option>[^\s].*)'
  143.         r'\s*(?P<vi>[:=])\s*'
  144.         r'(?P<value>.*)$'
  145.         )
  146.     optionxform = str
  147.  
  148. class PyCentralError(Exception):
  149.     """Python Central Exception"""
  150.     pass
  151.  
  152. class PyCentralVersionMissingError(PyCentralError):
  153.     """Python Central Version Missing Exception"""
  154.     pass
  155.  
  156. class PythonRuntime:
  157.     def __init__(self, name, version, interp, prefix):
  158.         self.name = name
  159.         self.version = version
  160.         if name.startswith('python'):
  161.             self.short_name = name[6:]
  162.         else:
  163.             self.short_name = name
  164.         self.interp = interp
  165.         if version in ('2.3', '2.4', '2.5'):
  166.             sitedir = 'site-packages/'
  167.         else:
  168.             sitedir = 'dist-packages/'
  169.         if prefix.endswith('/'):
  170.             self.prefix = prefix + sitedir
  171.         else:
  172.             self.prefix = prefix + '/' + sitedir
  173.  
  174.     def byte_compile_dirs(self, dirs, bc_option, exclude=None):
  175.         """call compileall.py -x <exclude regexp> <dirs> according
  176.         to bc_options"""
  177.         
  178.         logging.debug('\tbyte-compile directories')
  179.         errors = False
  180.         cmd = [self.interp, self.prefix + '/compileall.py', '-q']
  181.         if exclude:
  182.             cmd.extend(['-x', exclude])
  183.         cmd.extend(dirs)
  184.         for opt in ('standard', 'optimize'):
  185.             if not opt in bc_option:
  186.                 continue
  187.             if opt == 'optimize':
  188.                 cmd[1:1] = ['-O']
  189.             rv = os.spawnv(os.P_WAIT, self.interp, cmd[1:])
  190.             if rv:
  191.                 raise PyCentralError
  192.  
  193.     def byte_compile(self, files, bc_option, exclude=None, ignore_errors=False, force=False):
  194.         errors = False
  195.         if exclude:
  196.             rx = re.compile(exclude)
  197.             files2 = []
  198.             for fn in files:
  199.                 mo = rx.search(fn)
  200.                 if mo:
  201.                     continue
  202.                 files2.append(fn)
  203.         else:
  204.             files2 = files
  205.         if not files2:
  206.             logging.info('\tno files to byte-compile')
  207.             return
  208.         logging.debug('\tbyte-compile files (%d/%d) %s' \
  209.                       % (len(files), len(files2), self.name))
  210.         debug_files = files2[:min(2, len(files2))]
  211.         if len(files2) > 2:
  212.             debug_files.append('...')
  213.         logging.debug('\t    %s' % debug_files)
  214.         if 'python3' in self.interp:
  215.           cmd = [self.interp, '/usr/bin/py3_compilefiles', '-q']
  216.         else:
  217.           cmd = [self.interp, '/usr/bin/py_compilefiles', '-q']
  218.         if ignore_errors:
  219.             cmd.append('-i')
  220.         if force:
  221.             cmd.append('-f')
  222.         cmd.append('-')
  223.         for opt in ('standard', 'optimize'):
  224.             if not opt in bc_option:
  225.                 continue
  226.             if opt == 'optimize':
  227.                 cmd[1:1] = ['-O']
  228.             try:
  229.                 import subprocess
  230.                 p = subprocess.Popen(cmd, bufsize=1,
  231.                                      shell=False, stdin=subprocess.PIPE)
  232.                 fd = p.stdin
  233.             except ImportError:
  234.                 p = None
  235.                 fd = os.popen(' '.join(cmd), 'w')
  236.             for fn in files2:
  237.                 fd.write(fn + '\n')
  238.             rv = fd.close()
  239.             if p:
  240.                 p.wait()
  241.                 errors = p.returncode != 0
  242.             else:
  243.                 errors = rv != None
  244.             if errors:
  245.                 raise PyCentralError, 'error byte-compiling files (%d)' % len(files2)
  246.  
  247.     def remove_byte_code(self, files):
  248.         errors = False
  249.         logging.debug('\tremove byte-code files (%d)' % (len(files)))
  250.         for ext in ('c', 'o'):
  251.             for fn in files:
  252.                 fnc = fn + ext
  253.                 if os.path.exists(fnc):
  254.                     try:
  255.                         os.unlink(fnc)
  256.                     except OSError, e:
  257.                         print "Sorry", e
  258.                         errors = True
  259.         if errors:
  260.             raise PyCentralError, 'error removing the byte-code files'
  261.  
  262.     def list_byte_code(self, files):
  263.         logging.debug('\tlist byte-code files (%d)' % (len(files)))
  264.         for ext in ('c', 'o'):
  265.             for fn in files:
  266.                 fnc = fn + ext
  267.         yield fnc
  268.  
  269. installed_runtimes = None
  270. default_runtime = None
  271.  
  272. def get_installed_runtimes(with_unsupported=True):
  273.     global installed_runtimes
  274.     global default_runtime
  275.  
  276.     if not installed_runtimes:
  277.         import glob
  278.         installed_runtimes = []
  279.         default_version = pyversions.default_version(version_only=True)
  280.         supported = pyversions.supported_versions()
  281.         old = pyversions.old_versions()
  282.         if with_unsupported:
  283.             unsupported = pyversions.unsupported_versions()
  284.         else:
  285.             unsupported = []
  286.         for interp in glob.glob('/usr/bin/python[0-9].[0-9]'):
  287.             if old and os.path.basename(interp) in old:
  288.                 pass
  289.                 #print "INFO: using old version '%s'" % interp
  290.             elif unsupported and os.path.basename(interp) in unsupported:
  291.                 print "INFO: using unsupported version '%s'" % interp
  292.             if not (os.path.basename(interp) in supported
  293.                     or (old and os.path.basename(interp) in old)
  294.                     or (unsupported and os.path.basename(interp) in unsupported)):
  295.                 # pycentral used to ignore everything it has no explicit
  296.                 # knowledge about. this causes install errors when
  297.                 # debian_defaults is not up-to-date (LP: #354228)
  298.                 if with_unsupported:
  299.                     if not interp.startswith('/usr/bin/python3'):
  300.                         print "INFO: using unknown version '%s' (debian_defaults not up-to-date?)" % interp
  301.                 else:
  302.                     print "INFO: ignoring unknown version '%s'" % interp
  303.                     continue
  304.             version = interp[-3:]
  305.             rt = PythonRuntime('python' + version,
  306.                                version,
  307.                                '/usr/bin/python' + version,
  308.                                '/usr/lib/python' + version)
  309.             installed_runtimes.append(rt)
  310.             if version == default_version:
  311.                 default_runtime = rt
  312.     return installed_runtimes
  313.  
  314. def get_default_runtime():
  315.     get_installed_runtimes()
  316.     return default_runtime
  317.  
  318. def get_runtime_for_version(version):
  319.     if version == 'current':
  320.         return get_default_runtime()
  321.     for rt in get_installed_runtimes():
  322.         if rt.version == version:
  323.             return rt
  324.     return None
  325.     
  326. debian_config = None
  327. def get_debian_config():
  328.     global debian_config
  329.     if debian_config is not None:
  330.         return debian_config
  331.  
  332.     config = SafeConfigParser()
  333.     fn = '/etc/python/debian_config'
  334.     if os.path.exists(fn):
  335.         try:
  336.             config.readfp(open(fn))
  337.         except Error:
  338.             logging.error("error reading config file `%s'" % fn)
  339.             sys.exit(1)
  340.     # checks
  341.     if not config.has_option('DEFAULT', 'byte-compile'):
  342.         config.set('DEFAULT', 'byte-compile', 'standard')
  343.     bc_option = config.get('DEFAULT', 'byte-compile')
  344.     bc_values = set([v.strip() for v in bc_option.split(',')])
  345.     bc_unknown = bc_values - set(['standard', 'optimize'])
  346.     if bc_unknown:
  347.         sys.stderr.write("%s: `%s': unknown values `%s'"
  348.                          " in `byte-compile option'\n"
  349.                          % (program, fn, ', '.join(list(bc_unknown))))
  350.         sys.exit(1)
  351.     config.set('DEFAULT', 'byte-compile', ', '.join(bc_values))
  352.     if config.has_option('DEFAULT', 'overwrite-local'):
  353.         val = config.get('DEFAULT', 'overwrite-local').strip().lower()
  354.         overwrite_local = val in ('yes', '1', 'true')
  355.     else:
  356.         overwrite_local = False
  357.     config.set('DEFAULT', 'overwrite-local', overwrite_local and '1' or '0')
  358.     debian_config = config
  359.     return debian_config
  360.  
  361. class DebPackage:
  362.     def __init__(self, kind, name,
  363.                  oldstyle=False, default_runtime=None,
  364.                  pkgdir=None, pkgconfig=None, parse_versions=True):
  365.         self.kind = kind
  366.         self.name = name
  367.         self.version_field = None
  368.         self.oldstyle = oldstyle
  369.         self.parse_versions = parse_versions
  370.         self.default_runtime = default_runtime
  371.         self.shared_prefix = None
  372.         self.pkgdir = pkgdir
  373.         self.pkgconfig = pkgconfig
  374.         self.has_shared_extension = {}
  375.         self.has_private_extension = False
  376.         self.has_shared_module = {}
  377.         self.has_private_module = False
  378.         if pkgdir:
  379.             self.read_control()
  380.         else:
  381.             self.read_pyfiles()
  382.             #self.print_info()
  383.  
  384.     def read_pyfiles(self):
  385.         self.shared_files = []
  386.         self.pylib_files = {}
  387.         self.other_pylib_files = []
  388.         self.private_files = []
  389.         self.omitted_files = []
  390.         self.pysupport_files = []
  391.         if self.pkgdir:
  392.             lines = []
  393.             for root, dirs, files in os.walk(self.pkgdir):
  394.                 if root.endswith('DEBIAN'):
  395.                     continue
  396.                 if root != self.pkgdir:
  397.                     d = root[len(self.pkgdir):]
  398.                     lines.append(d)
  399.                     for name in files:
  400.                         lines.append(os.path.join(d, name))
  401.                 else:
  402.                     pass # no files in /
  403.         else:
  404.             config_file = '/usr/share/pyshared-data/%s' % self.name
  405.             if self.pkgconfig:
  406.                 lines = [fn for fn, t in self.pkgconfig.items('files')]
  407.                 lines.sort()
  408.             elif os.path.isfile(config_file):
  409.                 logging.debug("reading %s" % config_file)
  410.                 self.pkgconfig = PyCentralConfigParser()
  411.                 self.pkgconfig.optionxform = str
  412.                 self.pkgconfig.readfp(file(config_file))
  413.                 if not self.pkgconfig.has_option('pycentral', 'include-links'):
  414.                     self.pkgconfig.set('pycentral', 'include-links', '0')
  415.                 elif self.pkgconfig.get('pycentral', 'include-links') == '':
  416.                     # LP #332532, empty value in file
  417.                     self.pkgconfig.set('pycentral', 'include-links', '0')
  418.                 lines = [fn for fn, t in self.pkgconfig.items('files')]
  419.                 lines.sort()
  420.             else:
  421.                 lines = self.read_preinst_pkgconfig()
  422.             if lines:
  423.                 pass
  424.             elif os.environ.has_key("PYCENTRAL_NO_DPKG_QUERY"):
  425.                 logging.debug("Not using dpkg-query as requested")
  426.                 lines = map(string.strip, open('/var/lib/dpkg/info/%s.list' % self.name).readlines())
  427.             else:
  428.                 cmd = ['/usr/bin/dpkg-query', '-L', self.name]
  429.                 try:
  430.                     import subprocess
  431.                     p = subprocess.Popen(cmd, bufsize=1,
  432.                                          shell=False, stdout=subprocess.PIPE)
  433.                     fd = p.stdout
  434.                 except ImportError:
  435.                     fd = os.popen(' '.join(cmd))
  436.                 lines = [s[:-1] for s in fd.readlines()]
  437.             if not self.pkgconfig:
  438.                 pc = PyCentralConfigParser()
  439.                 pc.optionxform = str
  440.                 pc.add_section('python-package')
  441.                 pc.set('python-package', 'format', '1')
  442.                 pc.add_section('pycentral')
  443.                 pc.set('pycentral', 'version', req_pycentral_version)
  444.                 pc.set('pycentral', 'include-links', '0')
  445.                 pc.add_section('files')
  446.                 for line in lines:
  447.                     if os.path.isdir(line) and not os.path.islink(line):
  448.                         pc.set('files', line, 'd')
  449.                     elif os.path.exists(line):
  450.                         pc.set('files', line, 'f')
  451.                     else:
  452.                         pass # should not happen
  453.                 self.pkgconfig = pc
  454.  
  455.         old_shared_base = shared_base + self.name + '/site-packages/'
  456.         found_old_base = found_base2 = False
  457.         for line in lines:
  458.             fn = line
  459.             if fn.startswith(shared_base2):
  460.                 # keep _all_ files and directories
  461.                 self.shared_files.append(fn)
  462.                 if fn.endswith('.py'):
  463.                     self.has_shared_module['all'] = True
  464.                 found_base2 = True
  465.                 self.shared_prefix = shared_base2
  466.                 continue
  467.             elif fn.startswith(old_shared_base):
  468.                 # keep _all_ files and directories
  469.                 self.shared_files.append(fn)
  470.                 if fn.endswith('.py'):
  471.                     self.has_shared_module['all'] = True
  472.                 found_old_base = True
  473.                 self.shared_prefix = old_shared_base
  474.                 continue
  475.             elif fn.startswith('/usr/share/python-support') \
  476.                      or fn.startswith('/usr/lib/python-support'):
  477.                 self.pysupport_files.append(fn)
  478.                 continue
  479.             elif not fn.endswith('.py') and not fn.endswith('.so'):
  480.                 if re.match(r'/usr/lib/python\d\.\d/', fn):
  481.                     self.other_pylib_files.append(fn)
  482.                 continue
  483.             elif fn.startswith('/etc/') or fn.startswith('/usr/share/doc/'):
  484.                 # omit files in /etc and /usr/share/doc
  485.                 self.omitted_files.append(fn)
  486.                 continue
  487.             elif re.search(r'/s?bin/', fn):
  488.                 # omit files located in directories
  489.                 self.omitted_files.append(fn)
  490.                 continue
  491.             elif fn.startswith('/usr/lib/site-python/'):
  492.                 version = pyversions.default_version(version_only=True)
  493.                 self.pylib_files.setdefault(version, []).append(fn)
  494.                 continue
  495.             elif re.match(r'/usr/lib/python\d\.\d/', fn):
  496.                 version = fn[15:18]
  497.                 if fn.endswith('.so'):
  498.                     self.has_shared_extension[version] = True
  499.                 if fn.endswith('.py'):
  500.                     self.has_shared_module[version] = True
  501.                     self.pylib_files.setdefault(version, []).append(fn)
  502.                 continue
  503.             else:
  504.                 self.private_files.append(fn)
  505.                 if fn.endswith('.py'):
  506.                     self.has_private_module = True
  507.  
  508.         if found_old_base and found_base2:
  509.             raise PyCentralError, \
  510.                   'shared files found in old (%s) and new locations (%s)' % (old_shared_base, shared_base2)
  511.  
  512.     def read_preinst_pkgconfig(self):
  513.         try:
  514.             fd = open('/var/lib/dpkg/info/%s.preinst' % self.name, 'r')
  515.         except:
  516.             return None
  517.         buffer = None
  518.         for line in fd.readlines():
  519.             if line == '[python-package]\n':
  520.                 buffer = cStringIO.StringIO()
  521.             if line in ('PYEOF\n', 'EOF\n'):
  522.                 break
  523.             if buffer:
  524.                 buffer.write(line)
  525.         if buffer is None:
  526.             return None
  527.         self.pkgconfig = PyCentralConfigParser()
  528.         self.pkgconfig.optionxform = str
  529.         self.pkgconfig.readfp(cStringIO.StringIO(buffer.getvalue()))
  530.         # never set in the preinst copy of the config file
  531.         self.pkgconfig.set('pycentral', 'include-links', '0')
  532.         files = [fn for fn, t in self.pkgconfig.items('files')]
  533.         files.sort()
  534.         return files
  535.  
  536.     def print_info(self, fd=sys.stdout):
  537.         fd.write('Package: %s\n' % self.name)
  538.         fd.write('    shared files  :%4d\n' % len(self.shared_files))
  539.         fd.write('    private files :%4d\n' % len(self.private_files))
  540.         for ver, files in self.pylib_files.items():
  541.             fd.write('    pylib%s files:%4d\n' % (ver, len(files)))
  542.  
  543.     def read_control(self):
  544.         """read the debian/control file, extract the XS-Python-Version
  545.         field; check that XB-Python-Version exists for the package."""
  546.         if not os.path.exists('debian/control'):
  547.             raise PyCentralError, "debian/control not found"
  548.         self.version_field = None
  549.         self.sversion_field = None
  550.         try:
  551.             section = None
  552.             for line in file('debian/control'):
  553.                 line = line.strip()
  554.                 if line == '':
  555.                     section = None
  556.                 elif line.startswith('Source:'):
  557.                     section = 'Source'
  558.                 elif line.startswith('Package: ' + self.name):
  559.                     section = self.name
  560.                 elif line.startswith('XS-Python-Version:'):
  561.                     if section != 'Source':
  562.                         raise PyCentralError, \
  563.                               'attribute XS-Python-Version not in Source section'
  564.                     self.sversion_field = line.split(':', 1)[1].strip()
  565.                 elif line.startswith('XB-Python-Version:'):
  566.                     if section == self.name:
  567.                         self.version_field = line.split(':', 1)[1].strip()
  568.         except:
  569.             pass
  570.         if self.version_field == None:
  571.             raise PyCentralVersionMissingError, \
  572.                   'missing XB-Python-Version attribute in package %s' % self.name
  573.         if self.sversion_field == None:
  574.             raise PyCentralError, 'missing XS-Python-Version attribute'
  575.         if self.parse_versions:
  576.             self.sversion_info = pyversions.parse_versions(self.sversion_field)
  577.         else:
  578.             self.sversion_info = 'all' # dummy
  579.         self.has_private_extension = self.sversion_info == 'current_ext'
  580.  
  581.     def move_files(self):
  582.         """move files from the installation directory to the pycentral location"""
  583.         import shutil
  584.  
  585.         dsttop = self.pkgdir + shared_base2
  586.         try:
  587.             os.makedirs(dsttop)
  588.         except OSError:
  589.             pass
  590.         pversions = pyversions.supported_versions() \
  591.                     + pyversions.unsupported_versions() + pyversions.old_versions()
  592.         pversions = list(set(pversions))
  593.  
  594.         # rename .egg-info files and dirs, remove *.py[co] files
  595.         rename = 'norename' not in os.environ.get('DH_PYCENTRAL', '')
  596.         vrx = re.compile(r'(.*)(-py\d\.\d)(.*)(\.egg-info|\.pth)$')
  597.         for pversion in pversions:
  598.             if pversion in ('python2.3', 'python2.4', 'python2.5'):
  599.                 srctop = os.path.join(self.pkgdir, 'usr/lib', pversion, 'site-packages')
  600.             else:
  601.                 # srctop and srctop3 are the old/wrong locations, move these.
  602.                 srctop = os.path.join(self.pkgdir, 'usr/lib', pversion, 'site-packages')
  603.                 srctop2 = os.path.join(self.pkgdir, 'usr/lib', pversion, 'dist-packages')
  604.                 srctop3 = os.path.join(self.pkgdir, 'usr/local/lib', pversion, 'dist-packages')
  605.                 if os.path.isdir(srctop2) and (os.path.isdir(srctop) or os.path.isdir(srctop3)):
  606.                     raise PyCentralError, 'both directories site-packages and dist-packages exist.'
  607.                 if os.path.isdir(srctop):
  608.                     print 'renaming %s to %s' % (srctop, srctop2)
  609.                     os.rename(srctop, srctop2)
  610.                 elif os.path.isdir(srctop3):
  611.                     print 'renaming %s to %s' % (srctop3, srctop2)
  612.                     try:
  613.                         os.makedirs(os.path.dirname(srctop2))
  614.                     except OSError:
  615.                         pass
  616.                     os.rename(srctop3, srctop2)
  617.                     while srctop3:
  618.                         srctop3 = os.path.dirname(srctop3)
  619.                         try:
  620.                             os.rmdir(srctop3)
  621.                         except OSError:
  622.                             break
  623.                 srctop = srctop2
  624.                 # do the same with -dbg packages (which don't have an Python-Version attribute)
  625.                 pkgdbgdir = self.pkgdir + '-dbg'
  626.                 if os.path.isdir(pkgdbgdir):
  627.                     srcdtop = os.path.join(pkgdbgdir, 'usr/lib', pversion, 'site-packages')
  628.                     srcdtop2 = os.path.join(pkgdbgdir, 'usr/lib', pversion, 'dist-packages')
  629.                     srcdtop3 = os.path.join(pkgdbgdir, 'usr/local/lib', pversion, 'dist-packages')
  630.                     if os.path.isdir(srcdtop2) and (os.path.isdir(srcdtop) or os.path.isdir(srcdtop3)):
  631.                         print 'both directories site-packages and dist-packages exist.'
  632.                     if os.path.isdir(srcdtop):
  633.                         print 'renaming %s to %s' % (srcdtop, srcdtop2)
  634.                         os.rename(srcdtop, srcdtop2)
  635.                     elif os.path.isdir(srcdtop3):
  636.                         print 'renaming %s to %s' % (srcdtop3, srcdtop2)
  637.                         try:
  638.                             os.makedirs(os.path.dirname(srcdtop2))
  639.                         except OSError:
  640.                             pass
  641.                         os.rename(srcdtop3, srcdtop2)
  642.                         while srcdtop3:
  643.                             srcdtop3 = os.path.dirname(srcdtop3)
  644.                             try:
  645.                                 os.rmdir(srcdtop3)
  646.                             except OSError:
  647.                                 break
  648.                     srcdtop = os.path.join(pkgdbgdir, 'usr/lib/debug/usr/lib', pversion, 'site-packages')
  649.                     srcdtop2 = os.path.join(pkgdbgdir, 'usr/lib/debug/usr/lib', pversion, 'dist-packages')
  650.                     srcdtop3 = os.path.join(pkgdbgdir, 'usr/lib/debug/usr/local/lib', pversion, 'dist-packages')
  651.                     if os.path.isdir(srcdtop2) and (os.path.isdir(srcdtop) or os.path.isdir(srcdtop3)):
  652.                         print 'both directories site-packages and dist-packages exist.'
  653.                     if os.path.isdir(srcdtop):
  654.                         print 'renaming %s to %s' % (srcdtop, srcdtop2)
  655.                         os.rename(srcdtop, srcdtop2)
  656.                     elif os.path.isdir(srcdtop3):
  657.                         print 'renaming %s to %s' % (srcdtop3, srcdtop2)
  658.                         try:
  659.                             os.makedirs(os.path.dirname(srcdtop2))
  660.                         except OSError:
  661.                             pass
  662.                         os.rename(srcdtop3, srcdtop2)
  663.                         while srcdtop3:
  664.                             srcdtop3 = os.path.dirname(srcdtop3)
  665.                             try:
  666.                                 os.rmdir(srcdtop3)
  667.                             except OSError:
  668.                                 break
  669.             for root, dirs, files in os.walk(srctop, topdown=False):
  670.                 for name in files:
  671.                     m = vrx.match(name)
  672.                     if m and rename:
  673.                         name2 = ''.join(m.group(1, 3, 4))
  674.                         print "rename: %s -> %s" % (name, name2)
  675.                         os.rename(os.path.join(root, name), os.path.join (root, name2))
  676.                     elif name.endswith('.pyc') or name.endswith('.pyo'):
  677.                         os.unlink(os.path.join(root, name))
  678.                 for name in dirs:
  679.                     m = vrx.match(name)
  680.                     if m and rename:
  681.                         name2 = ''.join(m.group(1, 3, 4))
  682.                         print "rename: %s -> %s" % (name, name2)
  683.                         os.rename(os.path.join(root, name), os.path.join (root, name2))
  684.  
  685.         # search for differences
  686.         import filecmp
  687.         class MyDircmp(filecmp.dircmp):
  688.             def report(self):
  689.                 if self.left_only or self.right_only or self.diff_files or self.funny_files:
  690.                     self.differs = True
  691.                     print 'diff', self.left, self.right
  692.                 if self.left_only:
  693.                     self.left_only.sort()
  694.                     print 'Only in', self.left, ':', self.left_only
  695.                 if self.right_only:
  696.                     self.right_only.sort()
  697.                     print 'Only in', self.right, ':', self.right_only
  698.                 if self.diff_files:
  699.                     self.diff_files.sort()
  700.                     print 'Differing files :', self.diff_files
  701.                 if self.funny_files:
  702.                     self.funny_files.sort()
  703.                     print 'Trouble with common files :', self.funny_files
  704.  
  705.         for pv1 in pversions:
  706.             for pv2 in pversions[pversions.index(pv1)+1:]:
  707.                 site1 = os.path.join(self.pkgdir, third_party_dir(pv1))
  708.                 site2 = os.path.join(self.pkgdir, third_party_dir(pv2))
  709.                 if not (os.path.isdir(site1) and os.path.isdir(site2)):
  710.                     continue
  711.                 dco = MyDircmp(site1, site2)
  712.                 dco.differs = False
  713.                 dco.report_full_closure()
  714.                                  
  715.         # move around
  716.         for pversion in pversions:
  717.             srctop = os.path.join(self.pkgdir, third_party_dir(pversion))
  718.             for root, dirs, files in os.walk(srctop):
  719.                 if root == srctop:
  720.                     d = '.'
  721.                 else:
  722.                     d = root[len(srctop)+1:]
  723.                 for name in dirs:
  724.                     src = os.path.join(root, name)
  725.                     dst = os.path.join(dsttop, d, name)
  726.                     try:
  727.                         os.mkdir(dst)
  728.                         shutil.copymode(src, dst)
  729.                     except OSError:
  730.                         pass
  731.                 for name in files:
  732.                     src = os.path.join(root, name)
  733.                     dst = os.path.join(dsttop, d, name)
  734.                     if re.search(r'\.so(\.\d+)*?$', name):
  735.                         continue
  736.                     # TODO: if dst already exists, make sure, src == dst
  737.                     os.rename(src, dst)
  738.             # remove empty dirs in /usr/lib/pythonX.Y
  739.             for root, dirs, files in os.walk(self.pkgdir + '/usr/lib', topdown=False):
  740.                 try:
  741.             if re.match("/usr/lib/python\d\.\d($|/)", root.replace(self.pkgdir, "")):
  742.             os.rmdir(root)
  743.                 except OSError:
  744.                     pass
  745.             try:
  746.                 os.rmdir(self.pkgdir + '/usr/lib')
  747.             except OSError:
  748.                 pass
  749.         # remove empty dirs in /usr/share/pyshared
  750.         for root, dirs, files in os.walk(self.pkgdir + shared_base2, topdown=False):
  751.             try:
  752.                 os.rmdir(root)
  753.             except OSError:
  754.                 pass
  755.         try:
  756.             os.rmdir(self.pkgdir + shared_base2)
  757.         except OSError:
  758.             pass
  759.  
  760.     def add_shared_links(self):
  761.         versions = pyversions.requested_versions(self.sversion_field, True)
  762.         tgttop = os.path.join(self.pkgdir, shared_base2.strip('/'))
  763.         existing_links = []
  764.         for version in versions:
  765.             linktop = os.path.join(self.pkgdir, third_party_dir(version))
  766.             try:
  767.                 os.makedirs(linktop)
  768.             except OSError:
  769.                 pass
  770.             for root, dirs, files in os.walk(tgttop, topdown=True):
  771.                 linkroot = root.replace(tgttop, linktop, 1)
  772.                 for name in files:
  773.                     tgt = os.path.join(root, name)
  774.                     link = os.path.join(linkroot, name)
  775.                     if os.path.exists(link):
  776.                         existing_links.append(link)
  777.                     else:
  778.                         rel_tgt = build_relative_link(tgt, link)
  779.                         os.symlink(rel_tgt, link)
  780.                 for name in dirs:
  781.                     try:
  782.                         os.makedirs(os.path.join(linkroot, name))
  783.                     except OSError:
  784.                         pass
  785.         if existing_links:
  786.             print 'Found existing files while including symlinks:'
  787.             for link in existing_links:
  788.                 print '  ', link
  789.             raise PyCentralError, 'Found existing files while including symlinks'
  790.         
  791.     def gen_substvars(self):
  792.         supported = [d[6:] for d in pyversions.supported_versions()
  793.                      if re.match(r'python\d\.\d', d)]
  794.         versions = ''
  795.         prversions = ''
  796.         self.depends = None
  797.         if len(self.has_shared_module) or len(self.has_shared_extension):
  798.             # shared modules / extensions
  799.             if len(self.has_shared_extension):
  800.                 versions = self.has_shared_extension.keys()
  801.             else:
  802.                 if self.sversion_info in ('current', 'current_ext'):
  803.                     versions = 'current'
  804.                 elif self.sversion_info == 'all':
  805.                     versions = 'all'
  806.                     prversions = supported
  807.                 else:
  808.                     versions = self.sversion_field
  809.                     prversions = list(self.sversion_info.intersection(supported))
  810.                     self.depends = version2depends(self.sversion_info)
  811.         elif self.has_private_module or self.has_private_extension:
  812.             if self.sversion_info == 'all':
  813.                 versions = 'current'
  814.             elif self.sversion_info == 'current':
  815.                 versions = 'current'
  816.             elif self.sversion_info == 'current_ext':
  817.                 versions = [pyversions.default_version(version_only=True)]
  818.             elif isinstance(self.sversion_info, list) or isinstance(self.sversion_info, set):
  819.                 # exact version info required, no enumeration, no relops
  820.                 if len(self.sversion_info) != 1 or not re.match(r'\d\.\d', self.sversion_info[0]):
  821.                     raise PyCentralError, 'no exact version for package with private modules'
  822.                 versions = [list(self.sversion_info)[0]]
  823.             else:
  824.                 raise PyCentralError, 'version error for package with private modules'
  825.         else:
  826.             # just "copy" it from the source field
  827.             if self.sversion_info == 'current':
  828.                 versions = 'current'
  829.             elif self.sversion_info == 'current_ext':
  830.                 versions = [pyversions.default_version(version_only=True)]
  831.             elif self.sversion_info == 'all':
  832.                 versions = 'all'
  833.                 prversions = supported
  834.             else:
  835.                 versions = self.sversion_field
  836.                 prversions = list(self.sversion_info.intersection(supported))
  837.                 self.depends = version2depends(self.sversion_info)
  838.  
  839.         if (len(self.has_shared_module) or len(self.has_shared_extension)) \
  840.            and self.has_private_module or self.has_private_extension:
  841.             # use case? use the information for the shared stuff
  842.             pass
  843.         if versions == '':
  844.             raise PyCentralError, 'unable to determine Python-Version attribute'
  845.         if isinstance(versions, list) or isinstance(versions, set):
  846.             self.version_field = ', '.join(versions)
  847.         else:
  848.             self.version_field = versions
  849.         if not self.depends:
  850.             self.depends = version2depends(versions)
  851.         if self.name.startswith('python-'):
  852.             if prversions == '':
  853.                 prversions = versions
  854.             self.provides = ', '.join([self.name.replace('python-', 'python%s-' % ver)
  855.                                        for ver in prversions])
  856.     
  857.     def set_version_field(self, version_field):
  858.         self.version_field = version_field
  859.         if self.parse_versions:
  860.             self.version_info = pyversions.parse_versions(version_field)
  861.  
  862.     def read_version_info(self, use_default_if_missing=False):
  863.         """Read the Python-Version information field"""
  864.         if self.version_field:
  865.             return
  866.         if self.pkgconfig and self.pkgconfig.has_option('python-package', 'python-version'):
  867.             self.version_field = self.pkgconfig.get('python-package', 'python-version')
  868.             logging.debug("Using python-version from pkgconfig: %s" % self.version_field)
  869.         elif os.environ.has_key("PYCENTRAL_NO_DPKG_QUERY"):
  870.             logging.debug("Not using dpkg-query as requested")
  871.             needle = "Package: %s\n" % self.name
  872.             for block in open("/var/lib/dpkg/status").read().split("\n\n"):
  873.                 if needle in block:
  874.                     for line in block.split("\n"):
  875.                         if line.startswith('Python-Version:'):
  876.                             self.version_field = line.split(':', 1)[1].strip()
  877.                             break
  878.         else:
  879.             logging.debug("dpkg-query -s %s" % self.name)
  880.             cmd = ['/usr/bin/dpkg-query', '-s', self.name]
  881.             try:
  882.                 import subprocess
  883.                 p = subprocess.Popen(cmd, bufsize=1,
  884.                                      shell=False, stdout=subprocess.PIPE)
  885.                 fd = p.stdout
  886.             except ImportError:
  887.                 fd = os.popen(' '.join(cmd))
  888.             
  889.             for line in fd:
  890.                 if line.startswith('Python-Version:'):
  891.                     self.version_field = line.split(':', 1)[1].strip()
  892.                     break
  893.             fd.close()
  894.         # now verify/parse it
  895.         if not self.version_field and use_default_if_missing:
  896.             logging.warn("%s: has no Python-Version field, assuming default runtime" % self.name)
  897.             self.version_field = get_default_runtime().version
  898.         if not self.version_field:
  899.               
  900.             raise PyCentralError, "package has no field Python-Version"
  901.         if self.parse_versions:
  902.             self.version_info = pyversions.parse_versions(self.version_field)
  903.  
  904.     def set_default_runtime_from_version_info(self):
  905.         versions = list(pyversions.requested_versions(self.version_field, version_only=True))
  906.         if not versions:
  907.             #raise PyCentralError, "no matching runtime for `%s'" % self.version_field
  908.             logging.warn("%s: no matching runtime for `%s', using default"
  909.                          % (self.name, self.version_field))
  910.             self.default_runtime = get_default_runtime()
  911.         if len(versions) == 1:
  912.             self.default_runtime = get_runtime_for_version(versions[0])
  913.         elif pyversions.default_version(version_only=True) in versions:
  914.             self.default_runtime = get_default_runtime()
  915.         else:
  916.             self.default_runtime = get_runtime_for_version(versions[0])
  917.  
  918.     def byte_compile(self, runtimes, bc_option, exclude_regex, ignore_errors=False):
  919.         """byte compiles all files not handled by pycentral"""
  920.  
  921.         logging.debug("    byte-compile %s" % self.name)
  922.         if self.shared_files:
  923.             ppos = len(self.shared_prefix)
  924.             for rt in runtimes:
  925.                 linked_files = [ rt.prefix + fn[ppos:]
  926.                                  for fn in self.shared_files
  927.                                  if fn[-3:] == '.py']
  928.                 rt.byte_compile(linked_files, bc_option, exclude_regex, ignore_errors)
  929.  
  930.         for pyver, files in self.pylib_files.items():
  931.             logging.debug("bc for v%s (%d files)" % (pyver, len(files)))
  932.             rt = get_runtime_for_version(pyver)
  933.             if rt in runtimes:
  934.                 rt.byte_compile(files, bc_option, exclude_regex)
  935.  
  936.         if self.private_files:
  937.             logging.debug("bc %s private (%d files)" %
  938.                           (self.default_runtime.version, len(self.private_files)))
  939.             rt = self.default_runtime
  940.             rt.byte_compile(self.private_files, bc_option, exclude_regex)
  941.  
  942.     def remove_bytecode(self):
  943.         """remove all byte-compiled files not handled by pycentral"""
  944.         
  945.         assert self.oldstyle
  946.  
  947.         logging.debug("    remove byte-code for %s" % self.name)
  948.         pyfiles = []
  949.         for files in self.pylib_files.values():
  950.             pyfiles.extend(files)
  951.         pyfiles.extend(self.private_files)
  952.  
  953.         errors = False
  954.         for ext in ('c', 'o'):
  955.             for fn in pyfiles:
  956.                 fnc = fn + ext
  957.                 if os.path.exists(fnc):
  958.                     try:
  959.                         os.unlink(fnc)
  960.                     except OSError, e:
  961.                         print "Sorry", e
  962.                         errors = True
  963.         if errors:
  964.             raise PyCentralError
  965.  
  966.     def link_shared_files(self, rt):
  967.         """link shared files at runtime"""
  968.         #if samefs(rt.prefix, self.shared_files[0]):
  969.         #    link_cmd = os.link
  970.         #else:
  971.         #    link_cmd = os.symlink
  972.         logging.debug("\tlink shared files %s/%s" % (rt.name, self.name))
  973.         if not self.shared_files:
  974.             return []
  975.         link_cmd = os.symlink
  976.         ppos = len(self.shared_prefix)
  977.         existing_files = []
  978.         for fn in self.shared_files:
  979.             fn2 = rt.prefix + fn[ppos:]
  980.             if os.path.isdir(fn) and not os.path.islink(fn):
  981.                 continue
  982.             if os.path.exists(fn2):
  983.                 link = abs_link = None
  984.                 if os.path.islink(fn2):
  985.                     link = abs_link = os.readlink(fn2)
  986.                     if link.startswith('../'):
  987.                         abs_link = os.path.normpath(os.path.join(os.path.dirname(fn2), link))
  988.                 if abs_link == fn or link == fn:
  989.                     continue
  990.                 if not link or not (abs_link.startswith(shared_base2) or abs_link.startswith(shared_base)):
  991.                     existing_files.append(fn2)
  992.  
  993.         if existing_files:
  994.             conf = get_debian_config()
  995.             overwrite_local = conf.get('DEFAULT', 'overwrite-local') == '1'
  996.             if overwrite_local:
  997.                 print '\n  '.join(["overwriting local files:"] + existing_files)
  998.  
  999.         linked_files = []
  1000.         try:
  1001.             for fn in self.shared_files:
  1002.                 fn2 = rt.prefix + fn[ppos:]
  1003.                 if os.path.isdir(fn) and not os.path.islink(fn):
  1004.                     if os.path.isdir(fn2):
  1005.                         continue
  1006.                     os.makedirs(fn2)
  1007.                     linked_files.append(fn2)
  1008.                 else:
  1009.                     if os.path.exists(fn2):
  1010.                         msg = "already exists: %s" % fn2
  1011.                         link = abs_link = None
  1012.                         if os.path.islink(fn2):
  1013.                             link = abs_link = os.readlink(fn2)
  1014.                             if link.startswith('../'):
  1015.                                 abs_link = os.path.normpath(os.path.join(os.path.dirname(fn2), link))
  1016.                         if abs_link == fn or link == fn:
  1017.                             linked_files.append(fn2)
  1018.                             continue
  1019.                         if not link or not (abs_link.startswith(shared_base2) or abs_link.startswith(shared_base)):
  1020.                             msg = msg + " -> %s" % link
  1021.                             if overwrite_local:
  1022.                                 print "warning:", msg
  1023.                                 os.unlink(fn2)
  1024.                             else:
  1025.                                 continue # raise PyCentralError, msg at end of method
  1026.                     # make sure that fn2 really does not exist; this is a
  1027.                     # special hack to make pycentral work with fakechroot,
  1028.                     # which has a slightly weird treatment of symlinks
  1029.                     # now needed to switch between old and new prefix
  1030.                     try:
  1031.                         os.unlink(fn2)
  1032.                     except OSError:
  1033.                         pass
  1034.                     link_cmd(fn, fn2)
  1035.                     linked_files.append(fn2)
  1036.         except PyCentralError, msg:
  1037.             raise
  1038.         except Exception, msg:
  1039.             print msg
  1040.             # FIXME: undo
  1041.             linked_files.reverse()
  1042.             return []
  1043.         else:
  1044.             if existing_files and not overwrite_local:
  1045.                 s = get_file_overwrite_error(existing_files)
  1046.                 raise PyCentralError, s
  1047.             return linked_files
  1048.  
  1049.     def unlink_shared_files(self, rt):
  1050.         logging.debug('\tunlink_shared_files %s/%s' % (rt.name, self.name))
  1051.         if not self.shared_files:
  1052.             return
  1053.         ppos = len(self.shared_prefix)
  1054.         shared_files = self.shared_files[:]
  1055.         shared_files.reverse()
  1056.         for fn in shared_files:
  1057.             fn2 = rt.prefix + fn[ppos:]
  1058.             if os.path.isdir(fn2) and not os.path.islink(fn2):
  1059.                 try:
  1060.                     os.removedirs(fn2)
  1061.                 except OSError:
  1062.                     pass
  1063.             else:
  1064.                 if os.path.exists(fn2):
  1065.                     os.unlink(fn2)
  1066.  
  1067.     def list_shared_files(self, rt):
  1068.         logging.debug('\tlist_shared_files %s/%s' % (rt.name, self.name))
  1069.         if not self.shared_files:
  1070.             return
  1071.         ppos = len(self.shared_prefix)
  1072.         for fn in self.shared_files:
  1073.             fn2 = rt.prefix + fn[ppos:]
  1074.         yield fn2
  1075.  
  1076.  
  1077.     def install(self, runtimes, bc_option, exclude_regex,
  1078.                 byte_compile_default=True, ignore_errors=False):
  1079.         logging.debug('\tinstall package %s' % self.name)
  1080.         # install shared .py files
  1081.         symlinks_included = self.pkgconfig.getboolean('pycentral', 'include-links')
  1082.         if symlinks_included:
  1083.             pass
  1084.         else:
  1085.             if self.shared_files:
  1086.                 for rt in runtimes:
  1087.                     linked_files = self.link_shared_files(rt)
  1088.                     rt.byte_compile(linked_files, bc_option, exclude_regex, ignore_errors)
  1089.         # byte compile files inside prefix
  1090.         if self.pylib_files:
  1091.             for pyver, files in self.pylib_files.items():
  1092.                 rt = get_runtime_for_version(pyver)
  1093.                 if rt in runtimes:
  1094.                     rt.byte_compile(files, bc_option, exclude_regex, ignore_errors)
  1095.         # byte compile with the default runtime for the package
  1096.         if byte_compile_default:
  1097.             if self.private_files:
  1098.                 self.default_runtime.byte_compile(self.private_files, bc_option,
  1099.                                                   exclude_regex, ignore_errors)
  1100.  
  1101.     def prepare(self, runtimes, old_runtimes, old_pkg, ignore_errors=False):
  1102.         logging.debug('\tprepare package %s' % self.name)
  1103.  
  1104.         if old_pkg and old_pkg.private_files:
  1105.             fs_in_old = set([fn for fn in old_pkg.private_files if fn[-3:] == '.py'])
  1106.             fs_in_new = set([fn for fn in self.private_files if fn[-3:] == '.py'])
  1107.             removed_fs = list(fs_in_old.difference(fs_in_new))
  1108.             if removed_fs:
  1109.                 logging.debug_list('\t', 'removed private', removed_fs)
  1110.                 default_runtime.remove_byte_code(removed_fs)
  1111.  
  1112.         old_pylib_fs = []
  1113.         if old_pkg and old_pkg.pylib_files:
  1114.             for pyver, files in old_pkg.pylib_files.items():
  1115.                 fs_in_old = set([fn for fn in files if fn[-3:] == '.py'])
  1116.                 fs_in_new = set([fn for fn in self.pylib_files.get(pyver, []) if fn[-3:] == '.py'])
  1117.                 removed_fs = list(fs_in_old.difference(fs_in_new))
  1118.                 if removed_fs:
  1119.                     logging.debug_list('\t', 'removed pylib', removed_fs)
  1120.                     default_runtime.remove_byte_code(removed_fs)
  1121.                 old_pylib_fs += files
  1122.             old_pylib_fs += old_pkg.other_pylib_files
  1123.  
  1124.         if old_pkg and old_pkg.shared_files:
  1125.             for rt in old_runtimes:
  1126.                 if rt in runtimes:
  1127.                     continue
  1128.                 linked_files = [ rt.prefix + fn[ppos:]
  1129.                                  for fn in old_pkg.shared_files if fn[-3:] == '.py']
  1130.                 if linked_files:
  1131.                     logging.debug_list('\t', 'removed runtimes', linked_files)
  1132.                     default_runtime.remove_byte_code(linked_files)
  1133.                     self.unlink_files(rt)
  1134.  
  1135.         if not self.shared_files:
  1136.             return
  1137.  
  1138.         dirs_in_new = set([fn for fn, t in self.pkgconfig.items('files')
  1139.                            if t == 'd' if fn.startswith(self.shared_prefix)])
  1140.         dirs_in_old = set()
  1141.         if old_pkg:
  1142.             dirs_in_old = set([fn for fn, t in old_pkg.pkgconfig.items('files')
  1143.                                if t == 'd' if fn.startswith(self.shared_prefix)])
  1144.         new_dirs = list(dirs_in_new.difference(dirs_in_old))
  1145.         new_dirs.sort()
  1146.         removed_dirs = list(dirs_in_old.difference(dirs_in_new))
  1147.         removed_dirs.sort()
  1148.         
  1149.         fs_in_new = set([fn for fn, t in self.pkgconfig.items('files')
  1150.                          if t == 'f' if fn.startswith(self.shared_prefix)])
  1151.         fs_in_old = set()
  1152.         if old_pkg:
  1153.             fs_in_old = set([fn for fn, t in old_pkg.pkgconfig.items('files')
  1154.                              if t == 'f' if fn.startswith(self.shared_prefix)])
  1155.         new_fs = list(fs_in_new.difference(fs_in_old))
  1156.         new_fs.sort()
  1157.         removed_fs = list(fs_in_old.difference(fs_in_new))
  1158.         removed_fs.sort()
  1159.  
  1160.         logging.debug_list('\t', 'new      dirs', new_dirs)
  1161.         logging.debug_list('\t', 'removed  dirs', removed_dirs)
  1162.         logging.debug_list('\t', 'new     files', new_fs)
  1163.         logging.debug_list('\t', 'removed files', removed_fs)
  1164.         
  1165.         link_cmd = os.symlink
  1166.         ppos = len(self.shared_prefix)
  1167.         existing_files = []
  1168.         for rt in runtimes:
  1169.             for f1 in new_fs:
  1170.                 f2 = rt.prefix + f1[ppos:]
  1171.                 if os.path.exists(f2):
  1172.                     link = abs_link = None
  1173.                     if os.path.islink(f2):
  1174.                         link = abs_link = os.readlink(f2)
  1175.                         if link.startswith('../'):
  1176.                             abs_link = os.path.normpath(os.path.join(os.path.dirname(f2), link))
  1177.                     if abs_link == f1 or link == f1:
  1178.                         continue
  1179.                     if not link or not (abs_link.startswith(shared_base2) or abs_link.startswith(shared_base)):
  1180.                         existing_files.append(f2)
  1181.         if existing_files:
  1182.             # if the current installed version does not use python-central
  1183.             # then having a file here is expected and harmless
  1184.             if not self.name in [p for (p,v) in read_dpkg_status()]:
  1185.                 logging.info("%s: upgrade from package version not using python-central" % self.name)
  1186.                 return
  1187.             # if all existing files are found in the old package in
  1188.             # /usr/lib/pythonX.Y/site-packages, and moved to the shared area,
  1189.             # do nothing.
  1190.             not_in_same_pkg = set(existing_files)
  1191.             if old_pkg:
  1192.                 not_in_same_pkg.difference_update(old_pylib_fs)
  1193.             if not_in_same_pkg == set():
  1194.                 logging.info("%s: upgrade from package version with unmoved files" % self.name)
  1195.                 return
  1196.             conf = get_debian_config()
  1197.             overwrite_local = conf.get('DEFAULT', 'overwrite-local') == '1'
  1198.             if overwrite_local:
  1199.                 print "overwriting local files"
  1200.  
  1201.         for rt in runtimes:
  1202.             for d1 in new_dirs:
  1203.                 d2 = rt.prefix + d1[ppos:]
  1204.                 if os.path.isdir(d2):
  1205.                     continue
  1206.                 os.makedirs(d2)
  1207.             for f1 in new_fs:
  1208.                 f2 = rt.prefix + f1[ppos:]
  1209.                 if os.path.exists(f2):
  1210.                     msg = "already exists: %s" % f2
  1211.                     link = abs_link = None
  1212.                     if os.path.islink(f2):
  1213.                         link = abs_link = os.readlink(f2)
  1214.                         if link.startswith('../'):
  1215.                             abs_link = os.path.normpath(os.path.join(os.path.dirname(f2), link))
  1216.                     if abs_link == f1 or link == f1:
  1217.                         continue
  1218.                     if not link or not (abs_link.startswith(shared_base2) or abs_link.startswith(shared_base)):
  1219.                         msg = msg + " -> %s" % link
  1220.                         if overwrite_local:
  1221.                             print "warning:", msg
  1222.                             os.unlink(f2)
  1223.                         else:
  1224.                             continue # raise PyCentralError, msg at end of loop
  1225.                 # hack to make pycentral work with fakechroot
  1226.                 # now needed to switch between old and new prefix
  1227.                 try:
  1228.                     os.unlink(f2)
  1229.                 except OSError:
  1230.                     pass
  1231.                 try:
  1232.                     d = os.path.dirname(f2)
  1233.                     if not os.path.isdir(d):
  1234.                         print "create directory %s" % d
  1235.                         os.makedirs(d)
  1236.                     link_cmd(f1, f2)
  1237.                 except OSError:
  1238.                     print "unable to create symlink %s" % f2
  1239.                     raise
  1240.         if existing_files and not overwrite_local:
  1241.             raise PyCentralError, "not overwriting local files"
  1242.  
  1243.         for rt in runtimes:
  1244.             for f1 in removed_fs:
  1245.                 f2 = rt.prefix + f1[ppos:]
  1246.                 try:
  1247.                     os.unlink(f2)
  1248.                     os.unlink(f2 + 'c')
  1249.                     os.unlink(f2 + 'o')
  1250.                 except OSError:
  1251.                     pass
  1252.             for d1 in removed_dirs:
  1253.                 d2 = rt.prefix + d1[ppos:]
  1254.                 try:
  1255.                     os.rmdir(d2)
  1256.                 except OSError:
  1257.                     pass
  1258.  
  1259.         return
  1260.  
  1261.     def remove(self, runtimes, remove_script_files=True):
  1262.         logging.debug('\tremove package %s' % self.name)
  1263.         # remove shared .py files
  1264.         symlinks_included = self.pkgconfig.getboolean('pycentral', 'include-links')
  1265.         if symlinks_included:
  1266.             pass
  1267.         else:
  1268.             if self.shared_files:
  1269.                 ppos = len(self.shared_prefix)
  1270.                 for rt in runtimes:
  1271.                     linked_files = [ rt.prefix + fn[ppos:]
  1272.                                      for fn in self.shared_files
  1273.                                      if fn[-3:] == '.py']
  1274.                     #print self.shared_files
  1275.                     #print linked_files
  1276.                     default_runtime.remove_byte_code(linked_files)
  1277.                     self.unlink_shared_files(rt)
  1278.         # remove byte compiled files inside prefix
  1279.         if self.pylib_files:
  1280.             for pyver, files in self.pylib_files.items():
  1281.                 rt = get_runtime_for_version(pyver)
  1282.                 if rt in runtimes:
  1283.                     default_runtime.remove_byte_code(files)
  1284.         # remove byte code for script files
  1285.         if remove_script_files:
  1286.             if self.private_files:
  1287.                 default_runtime.remove_byte_code(self.private_files)
  1288.  
  1289.     def list(self, runtimes, list_script_files=True):
  1290.         logging.debug('\tlist package %s' % self.name)
  1291.         # list shared .py files
  1292.         symlinks_included = self.pkgconfig.getboolean('pycentral', 'include-links')
  1293.         if not symlinks_included and self.shared_files:
  1294.             ppos = len(self.shared_prefix)
  1295.             for rt in runtimes:
  1296.                 linked_files = [ rt.prefix + fn[ppos:]
  1297.                                  for fn in self.shared_files
  1298.                                  if fn[-3:] == '.py']
  1299.         for f in default_runtime.list_byte_code(linked_files):
  1300.             yield f
  1301.         for f in self.list_shared_files(rt):
  1302.             yield f
  1303.         # list byte compiled files inside prefix
  1304.         if self.pylib_files:
  1305.             for pyver, files in self.pylib_files.items():
  1306.                 rt = get_runtime_for_version(pyver)
  1307.                 if rt in runtimes:
  1308.             for f in default_runtime.list_byte_code(files):
  1309.             yield f
  1310.         # list byte code for script files
  1311.         if list_script_files:
  1312.             if self.private_files:
  1313.         for f in default_runtime.list_byte_code(self.private_files):
  1314.             yield f
  1315.  
  1316.     def update_bytecode_files(self, runtimes, rt_default, bc_option):
  1317.         # byte-compile with default python version
  1318.         logging.debug('\tupdate byte-code for %s' % self.name)
  1319.         exclude_regex = None
  1320.         # update shared .py files
  1321.         if self.shared_files:
  1322.             ppos = len(self.shared_prefix)
  1323.             for rt in runtimes:
  1324.                 if rt == rt_default:
  1325.                     linked_files = self.link_shared_files(rt)
  1326.                     rt.byte_compile(linked_files, bc_option, exclude_regex)
  1327.                 else:
  1328.                     linked_files = [ rt.prefix + fn[ppos:]
  1329.                                      for fn in self.shared_files
  1330.                                      if fn[-3:] == '.py']
  1331.                     rt.remove_byte_code(linked_files)
  1332.                     self.unlink_shared_files(rt)
  1333.         # byte compile with the default runtime for the package
  1334.         if self.private_files:
  1335.             self.default_runtime.byte_compile(self.private_files,
  1336.                                               bc_option, exclude_regex, force=True)
  1337.  
  1338. known_actions = {}
  1339. def register_action(action_class):
  1340.     known_actions[action_class.name] = action_class
  1341.  
  1342. class Action:
  1343.     _option_parser = None
  1344.     name = None
  1345.     help = ""
  1346.     usage = "<options>"
  1347.     def __init__(self):
  1348.         self.errors_occured = 0
  1349.         parser = self.get_option_parser()
  1350.         parser.set_usage(
  1351.             'usage: %s [<options> ...] %s %s' % (program, self.name, self.usage))
  1352.  
  1353.     def get_option_parser(self):
  1354.         if not self._option_parser:
  1355.             p = OptionParser()
  1356.             self._option_parser = p
  1357.         return self._option_parser
  1358.  
  1359.     def info(self, msg, stream=sys.stderr):
  1360.         logging.info('%s %s: %s' % (program, self.name, msg))
  1361.  
  1362.     def warn(self, msg, stream=sys.stderr):
  1363.         logging.warn('%s %s: %s' % (program, self.name, msg))
  1364.  
  1365.     def error(self, msg, stream=sys.stderr, go_on=False):
  1366.         logging.error('%s %s: %s' % (program, self.name, msg))
  1367.         self.errors_occured += 1
  1368.         if not go_on:
  1369.             sys.exit(1)
  1370.  
  1371.     def parse_args(self, arguments):
  1372.         self.options, self.args = self._option_parser.parse_args(arguments)
  1373.         return self.options, self.args
  1374.  
  1375.     def check_args(self, global_options):
  1376.         return self.errors_occured
  1377.  
  1378.     def run(self, global_opts):
  1379.         pass
  1380.  
  1381.  
  1382. class ActionByteCompile(Action):
  1383.     """byte compile the *.py files in <package> using the the
  1384.     default python version (or use the version specified with -v.
  1385.     Any additional directory arguments are ignored (only files
  1386.     found in the package are byte compiled. Files in
  1387.     /usr/lib/pythonX.Y are compiled with the matching python version.
  1388.  
  1389.     bccompile is a replacement for the current byte compilation
  1390.     generated by the dh_python debhelper script.
  1391.     """
  1392.     name = 'bccompile'
  1393.     help = 'byte compile .py files in a package'
  1394.     usage = '[<options>] <package> [<dir> ...]'
  1395.  
  1396.     def get_option_parser(self):
  1397.         if not self._option_parser:
  1398.             p = OptionParser()
  1399.             p.add_option('-x', '--exclude',
  1400.                          help="skip files matching the regular expression",
  1401.                          default=None, action='store', dest='exclude')
  1402.             p.add_option('-V', '--version',
  1403.                          help="byte compile using this python version",
  1404.                          default='current', action='store', dest='version')
  1405.             self._option_parser = p
  1406.         return self._option_parser
  1407.  
  1408.     def check_args(self, global_options):
  1409.         if len(self.args) < 1:
  1410.             self._option_parser.print_help()
  1411.             sys.exit(1)
  1412.         self.pkgname = self.args[0]
  1413.         self.runtime = get_runtime_for_version(self.options.version)
  1414.         if not self.runtime:
  1415.             self.error("unknown runtime version %s" % self.options.version)
  1416.  
  1417.         if not os.path.exists('/var/lib/dpkg/info/%s.list' % self.pkgname):
  1418.             self.error("package %s is not installed" % self.pkgname)
  1419.         self.pkg = DebPackage('package', self.pkgname, oldstyle=False,
  1420.                               default_runtime=self.runtime)
  1421.         self.pkg.read_version_info()
  1422.         return self.errors_occured
  1423.  
  1424.     def run(self, global_options):
  1425.         logging.debug('bccompile %s' % self.pkgname)
  1426.         runtimes = get_installed_runtimes()
  1427.         config = get_debian_config()
  1428.         bc_option = config.get('DEFAULT', 'byte-compile')
  1429.         requested = pyversions.requested_versions_for_runtime(self.pkg.version_field, version_only=True)
  1430.         used_runtimes = [rt for rt in runtimes if rt.short_name in requested]
  1431.         # called with directories as arguments
  1432.         if 0 and self.directories:
  1433.             try:
  1434.                 for version, dirs in self.pylib_dirs.items():
  1435.                     rt = get_runtime_for_version(version)
  1436.                     rt.byte_compile_dirs(dirs, bc_option, self.options.exclude)
  1437.                 if self.private_dirs:
  1438.                     version = pkg.version_field
  1439.                     if version == 'current':
  1440.                         version = pyversions.default_version(version_only=True)
  1441.                     rt = get_runtime_for_version(version)
  1442.                     rt.byte_compile_dirs(private_dirs, bc_option, self.options.exclude)
  1443.             except PyCentralError:
  1444.                 self.error("error byte-compiling package `%s'" % self.pkgname)
  1445.             return
  1446.  
  1447.         try:
  1448.             self.pkg.byte_compile(used_runtimes, bc_option, self.options.exclude)
  1449.         except PyCentralError:
  1450.             self.error("error byte-compiling package `%s'" % self.pkgname)
  1451.  
  1452. register_action(ActionByteCompile)
  1453.  
  1454. class ActionPkgInstall(Action):
  1455.     name = 'pkginstall'
  1456.     help = 'make a package available for all supported runtimes'
  1457.     usage = '[<options>] <package>'
  1458.     
  1459.     def get_option_parser(self):
  1460.         if not self._option_parser:
  1461.             p = OptionParser()
  1462.             p.add_option('-x', '--exclude',
  1463.                          help="skip files matching the regular expression",
  1464.                          default=None, action='store', dest='exclude')
  1465.             self._option_parser = p
  1466.         return self._option_parser
  1467.  
  1468.     def check_args(self, global_options):
  1469.         if len(self.args) != 1:
  1470.             self._option_parser.print_help()
  1471.             sys.exit(1)
  1472.         self.pkgname = self.args[0]
  1473.         if not os.path.exists('/var/lib/dpkg/info/%s.list' % self.pkgname):
  1474.             self.error("package %s is not installed" % self.pkgname)
  1475.         return self.errors_occured
  1476.  
  1477.     def run(self, global_options):
  1478.         runtimes = get_installed_runtimes()
  1479.         config = get_debian_config()
  1480.         bc_option = config.get('DEFAULT', 'byte-compile')
  1481.         pkg = DebPackage('package', self.args[0], oldstyle=False)
  1482.         pkg.read_version_info()
  1483.         requested = pyversions.requested_versions_for_runtime(pkg.version_field, version_only=True)
  1484.         used_runtimes = [rt for rt in runtimes if rt.short_name in requested]
  1485.         try:
  1486.             pkg.set_default_runtime_from_version_info()
  1487.         except ValueError:
  1488.         # Package doesn't provide support for any supported runtime
  1489.         if len(used_runtimes) == 0:
  1490.         self.error('%s needs unavailable runtime (%s)'
  1491.                % (self.pkgname, pkg.version_field))
  1492.         else:
  1493.         # Still byte compile for the available runtimes (with the
  1494.         # first matching runtime)
  1495.          pkg.default_runtime = get_runtime_for_version(used_runtimes[0])
  1496.         logging.debug('\tavail=%s, pkg=%s, install=%s'
  1497.                       % ([rt.short_name for rt in runtimes],
  1498.                          pkg.version_field,
  1499.                          [rt.short_name for rt in used_runtimes]))
  1500.         try:
  1501.             pkg.install(used_runtimes, bc_option,
  1502.                         self.options.exclude, byte_compile_default=True)
  1503.         except PyCentralError, msg:
  1504.             self.error(msg)
  1505.             
  1506.  
  1507. register_action(ActionPkgInstall)
  1508.  
  1509.  
  1510. class ActionPkgPrepare(Action):
  1511.     name = 'pkgprepare'
  1512.     help = 'prepare a package for all supported runtimes'
  1513.     usage = '[<options>] <package>'
  1514.     
  1515.     def get_option_parser(self):
  1516.         if not self._option_parser:
  1517.             p = OptionParser()
  1518.             p.add_option('-x', '--exclude',
  1519.                          help="skip files matching the regular expression",
  1520.                          default=None, action='store', dest='exclude')
  1521.             self._option_parser = p
  1522.         return self._option_parser
  1523.  
  1524.     def check_args(self, global_options):
  1525.         if len(self.args) != 1:
  1526.             self._option_parser.print_help()
  1527.             sys.exit(1)
  1528.         self.pkgname = self.args[0]
  1529.         # FIXME: run from the preinst, package may not exist
  1530.         #if not os.path.exists('/var/lib/dpkg/info/%s.list' % self.pkgname):
  1531.         #    self.error("package %s is not installed" % self.pkgname)
  1532.         return self.errors_occured
  1533.  
  1534.     def run(self, global_options):
  1535.         # functionality disabled starting with 0.6.9
  1536.         return
  1537.     
  1538.         runtimes = get_installed_runtimes()
  1539.         config = get_debian_config()
  1540.         pkgconfig = PyCentralConfigParser()
  1541.         pkgconfig.optionxform = str
  1542.         pkgconfig.readfp(sys.stdin)
  1543.         version_field = pkgconfig.get('python-package', 'python-version')
  1544.         try:
  1545.             requested = pyversions.requested_versions_for_runtime(version_field, version_only=True)
  1546.         except pyversions.PyCentralEmptyValueError, msg:
  1547.             # cannot install yet; remove the symlinked files and byte code files from the old
  1548.             # version, rely on the pkginstall in the postinst.
  1549.             print "pycentral: required runtimes not yet installed, skip pkgprepare, call pkgremove"
  1550.             runtimes = get_installed_runtimes(with_unsupported=True)
  1551.             pkg = DebPackage('package', self.args[0], oldstyle=False)
  1552.             pkg.read_version_info()
  1553.             pkg.default_runtime = get_default_runtime()
  1554.             try:
  1555.                 pkg.remove(runtimes, remove_script_files=True)
  1556.             except PyCentralError, msg:
  1557.                 self.warn(msg)
  1558.             return
  1559.         used_runtimes = [rt for rt in runtimes if rt.short_name in requested]
  1560.         pkg = DebPackage('package', self.args[0], oldstyle=False, pkgconfig=pkgconfig)
  1561.         pkg.set_version_field(version_field)
  1562.         try:
  1563.             pkg.set_default_runtime_from_version_info()
  1564.         except ValueError:
  1565.         # Package doesn't provide support for any supported runtime
  1566.         if len(used_runtimes) == 0:
  1567.         self.error('%s needs unavailable runtime (%s)'
  1568.                % (self.pkgname, pkg.version_field))
  1569.         else:
  1570.         # Still byte compile for the available runtimes (with the
  1571.         # first matching runtime)
  1572.          pkg.default_runtime = get_runtime_for_version(used_runtimes[0])
  1573.  
  1574.         if os.path.exists('/var/lib/dpkg/info/%s.list' % self.pkgname):
  1575.             old_pkg = DebPackage('package', self.args[0], oldstyle=False)
  1576.             try:
  1577.                 old_pkg.read_version_info()
  1578.             except PyCentralError:
  1579.                 old_pkg.set_version_field(version_field)
  1580.                 
  1581.             old_requested = pyversions.requested_versions_for_runtime(old_pkg.version_field, version_only=True)
  1582.             old_used_runtimes = [rt for rt in runtimes if rt.short_name in requested]
  1583.         else:
  1584.             old_pkg = None
  1585.             old_used_runtimes = []
  1586.         logging.debug('\tavail=%s, pkg=%s, prepare=%s'
  1587.                       % ([rt.short_name for rt in runtimes],
  1588.                          version_field,
  1589.                          [rt.short_name for rt in used_runtimes]))
  1590.         try:
  1591.             pkg.prepare(used_runtimes, old_used_runtimes, old_pkg)
  1592.         except PyCentralError, msg:
  1593.             self.error(msg)
  1594.  
  1595. register_action(ActionPkgPrepare)
  1596.  
  1597.  
  1598. class ActionBCRemove(Action):
  1599.     """remove the byte-compiled files in <package>.
  1600.  
  1601.     bccompile is a replacement for the current byte compilation
  1602.     generated by the dh_python debhelper script.
  1603.     """
  1604.     name = 'bcremove'
  1605.     help = 'remove the byte compiled .py files'
  1606.     usage = '<package>'
  1607.  
  1608.     def check_args(self, global_options):
  1609.         if len(self.args) != 1:
  1610.             self._option_parser.print_help()
  1611.             sys.exit(1)
  1612.         self.pkgname = self.args[0]
  1613.         if not os.path.exists('/var/lib/dpkg/info/%s.list' % self.pkgname):
  1614.             self.error("package %s is not installed" % self.pkgname)
  1615.         return self.errors_occured
  1616.  
  1617.     def run(self, global_options):
  1618.         pkg = DebPackage('package', self.args[0], oldstyle=True)
  1619.         try:
  1620.             pkg.remove_bytecode()
  1621.         except PyCentralError, msg:
  1622.             self.error(msg)
  1623.  
  1624. register_action(ActionBCRemove)
  1625.  
  1626.  
  1627. class ActionPkgRemove(Action):
  1628.     """
  1629.     """
  1630.     name = 'pkgremove'
  1631.     help = 'remove a package installed for all supported runtimes'
  1632.     usage = '<package>'
  1633.  
  1634.     def check_args(self, global_options):
  1635.         if len(self.args) != 1:
  1636.             self._option_parser.print_help()
  1637.             sys.exit(1)
  1638.         self.pkgname = self.args[0]
  1639.         if not os.path.exists('/var/lib/dpkg/info/%s.list' % self.pkgname):
  1640.             self.error("package %s is not installed" % self.pkgname)
  1641.         return self.errors_occured
  1642.  
  1643.     def run(self, global_options):
  1644.         runtimes = get_installed_runtimes(with_unsupported=True)
  1645.         pkg = DebPackage('package', self.args[0], oldstyle=False)
  1646.         pkg.read_version_info(use_default_if_missing=True)
  1647.         try:
  1648.             pkg.set_default_runtime_from_version_info()
  1649.         except ValueError:
  1650.             # original runtime is already removed, use the default for removal
  1651.             pkg.default_runtime = get_default_runtime()
  1652.         try:
  1653.             pkg.remove(runtimes, remove_script_files=True)
  1654.         except PyCentralError, msg:
  1655.             self.error(msg)
  1656.  
  1657. register_action(ActionPkgRemove)
  1658.  
  1659. class ActionPkgList(Action):
  1660.     name = 'pkglist'
  1661.     help = 'list all pycentral-managed files for <package>'
  1662.     usage = '<package>'
  1663.  
  1664.     def check_args(self, global_options):
  1665.         if len(self.args) != 1:
  1666.             self._option_parser.print_help()
  1667.             sys.exit(1)
  1668.         self.pkgname = self.args[0]
  1669.         if not os.path.exists('/var/lib/dpkg/info/%s.list' % self.pkgname):
  1670.             self.error("package %s is not installed" % self.pkgname)
  1671.         return self.errors_occured
  1672.  
  1673.     def run(self, global_options):
  1674.         runtimes = get_installed_runtimes(with_unsupported=True)
  1675.         pkg = DebPackage('package', self.args[0], oldstyle=False)
  1676.         pkg.read_version_info()
  1677.         try:
  1678.             pkg.set_default_runtime_from_version_info()
  1679.         except ValueError:
  1680.             # original runtime may be removed, use the default
  1681.             pkg.default_runtime = get_default_runtime()
  1682.         try:
  1683.         for f in pkg.list(runtimes, list_script_files=True):
  1684.         print f
  1685.         except PyCentralError, msg:
  1686.             self.error(msg)
  1687.  
  1688. register_action(ActionPkgList)
  1689.  
  1690.  
  1691. class ActionRuntimeInstall(Action):
  1692.     name = 'rtinstall'
  1693.     help = 'make installed packages available for this runtime'
  1694.  
  1695.     def get_option_parser(self):
  1696.         if not self._option_parser:
  1697.             p = OptionParser()
  1698.             p.add_option('-i', '--ignore-errors',
  1699.                          help="ignore errors during byte compilations",
  1700.                          default=None, action='store', dest='ignore')
  1701.             self._option_parser = p
  1702.         return self._option_parser
  1703.  
  1704.     def check_args(self, global_options):
  1705.         if len(self.args) != 1:
  1706.             self._option_parser.print_help()
  1707.             sys.exit(1)
  1708.         self.rtname = self.args[0]
  1709.         if self.rtname[-8:] == '-minimal':
  1710.             self.rtname = self.rtname[:-8]
  1711.         self.runtime = None
  1712.         for rt in get_installed_runtimes():
  1713.             if rt.name == self.rtname:
  1714.                 self.runtime = rt
  1715.                 break
  1716.         if not self.runtime:
  1717.             self.error('installed runtime %s not found' % self.rtname)
  1718.         self.rtname_short = self.rtname[6:]
  1719.         return self.errors_occured
  1720.  
  1721.     def run(self, global_options):
  1722.         packages = [(p, v) for p, v in read_dpkg_status()
  1723.                     if not p in (self.rtname, self.rtname+'-minimal')]
  1724.         needed_packages = []
  1725.         for pkgname, vstring in packages:
  1726.             try:
  1727.                 requested = list(pyversions.requested_versions(vstring, version_only=True))
  1728.                 if not self.rtname_short in requested:
  1729.                     logging.info("%s not in what requested_versions() returned, adding it anyway" % self.rtname_short)
  1730.                     requested.append(self.rtname_short)
  1731.             except ValueError:
  1732.                 logging.info('\tunsupported for %s: %s (%s)' % (self.rtname, pkgname, vstring))
  1733.                 continue
  1734.             if self.runtime.short_name in requested:
  1735.                 needed_packages.append((pkgname, vstring, requested))
  1736.         logging.info('\t%d packages with Python-Version info installed, %d for %s'
  1737.                      % (len(packages), len(needed_packages), self.rtname))
  1738.  
  1739.         # XXX not needed for an upgrade of a runtime
  1740.         byte_compile_for_default = (self.runtime == default_runtime)
  1741.  
  1742.         bc_option = get_debian_config().get('DEFAULT', 'byte-compile')
  1743.         for pkgname, vstring, vinfo in needed_packages:
  1744.             try:
  1745.                 logging.info('\tsupport %s for %s' % (pkgname, self.rtname))
  1746.                 pkg = DebPackage('package', pkgname, oldstyle=False)
  1747.                 pkg.read_version_info()
  1748.                 try:
  1749.                     pkg.set_default_runtime_from_version_info()
  1750.                 except ValueError:
  1751.                     logging.warn('\t%s not available for %s (%s)'
  1752.                                  % (pkgname, self.rtname, pkg.version_field))
  1753.                 pkg.install([self.runtime], bc_option, None,
  1754.                             byte_compile_for_default,
  1755.                             ignore_errors = self.options.ignore != None)
  1756.             except PyCentralError, msg:
  1757.                 self.error('package %s: %s' % (pkgname, msg))
  1758.  
  1759. register_action(ActionRuntimeInstall)
  1760.  
  1761. class ActionRuntimeRemove(Action):
  1762.     name = 'rtremove'
  1763.     help = 'remove packages installed for this runtime'
  1764.  
  1765.     def check_args(self, global_options):
  1766.         if len(self.args) != 1:
  1767.             self._option_parser.print_help()
  1768.             sys.exit(1)
  1769.         self.rtname = self.args[0]
  1770.         if self.rtname[-8:] == '-minimal':
  1771.             self.rtname = self.rtname[:-8]
  1772.         self.runtime = None
  1773.         for rt in get_installed_runtimes(with_unsupported=True):
  1774.             if rt.name == self.rtname:
  1775.                 self.runtime = rt
  1776.                 break
  1777.         if not self.runtime:
  1778.             self.error('installed runtime %s not found' % self.rtname)
  1779.         return self.errors_occured
  1780.  
  1781.     def run(self, global_options):
  1782.         packages = [(p, v) for p, v in read_dpkg_status(verbose=True)
  1783.                     if not p in (self.rtname, self.rtname+'-minimal')]
  1784.         needed_packages = []
  1785.         import subprocess
  1786.         for pkgname, vstring in packages:
  1787.             if not os.path.exists('/var/lib/dpkg/info/%s.list' % pkgname):
  1788.                 # already removed, but /var/lib/dpkg/status not yet updated
  1789.                 continue
  1790.             cmd = ['/usr/bin/dpkg-query', '-W', '-f', '${Status}\n', pkgname]
  1791.             p = subprocess.Popen(cmd, bufsize=1,
  1792.                                  shell=False, stdout=subprocess.PIPE)
  1793.             fd = p.stdout
  1794.             status = fd.readline().strip().split()
  1795.             fd.close()
  1796.             if not 'installed' in status:
  1797.                 # already removed, but /var/lib/dpkg/status not yet updated
  1798.                 continue
  1799.             try:
  1800.                 requested = list(pyversions.requested_versions_for_runtime(vstring, version_only=True))
  1801.             except ValueError:
  1802.                 logging.info('\tunsupported for %s: %s (%s)' % (self.rtname, pkgname, vstring))
  1803.                 continue
  1804.             if self.runtime.short_name in requested:
  1805.                 needed_packages.append((pkgname, vstring, requested))
  1806.         logging.info('\t%d pycentral supported packages installed, %d for %s'
  1807.                      % (len(packages), len(needed_packages), self.rtname))
  1808.         failed = []
  1809.         for pkgname, vstring, vinfo in needed_packages:
  1810.             logging.info('\trtremove: remove package %s for %s' % (pkgname, self.rtname))
  1811.             pkg = DebPackage('package', pkgname)
  1812.             pkg.set_version_field(vstring)
  1813.             try:
  1814.                 pkg.set_default_runtime_from_version_info()
  1815.             except ValueError:
  1816.                 # original runtime is already removed, use the default for removal
  1817.                 pkg.default_runtime = get_default_runtime()
  1818.             try:
  1819.                 pkg.remove([self.runtime], remove_script_files=False)
  1820.             except PyCentralError, msg:
  1821.                 self.error('failed to remove %s support for package %s' % (self.rtname, pkgname), go_on=True)
  1822.                 failed.append(pkgname)
  1823.         if failed:
  1824.             self.error('failed to remove %s support for %d packages' % len(failed))
  1825.  
  1826. register_action(ActionRuntimeRemove)
  1827.  
  1828.  
  1829. class ActionUpdateDefault(Action):
  1830.     name = 'updatedefault'
  1831.     help = 'update the default python version'
  1832.     usage = '<old runtime> <new runtime>'
  1833.     
  1834.     def check_args(self, global_options):
  1835.         if len(self.args) != 2:
  1836.             self._option_parser.print_help()
  1837.             sys.exit(1)
  1838.         self.oldrtname = self.args[0]
  1839.         self.rtname = self.args[1]
  1840.         packages = read_dpkg_status()
  1841.         self.needed_packages = []
  1842.         for pkgname, vstring in packages:
  1843.             if vstring.find('current') == -1:
  1844.                 continue
  1845.             try:
  1846.                 versions = pyversions.requested_versions(vstring, version_only=True)
  1847.             except ValueError:
  1848.                 self.error("package %s is not ready to be updated for %s"
  1849.                            % (pkgname, self.rtname))
  1850.                 continue
  1851.             pkg = DebPackage('package', pkgname)
  1852.             self.needed_packages.append(pkg)
  1853.         return self.errors_occured
  1854.  
  1855.     def run(self, global_options):
  1856.         logging.info('\tupdate default: update %d packages for %s'
  1857.                      % (len(self.needed_packages), self.rtname))
  1858.         runtimes = get_installed_runtimes()
  1859.         default_rt = get_default_runtime()
  1860.         bc_option = get_debian_config().get('DEFAULT', 'byte-compile')
  1861.         try:
  1862.             for pkg in self.needed_packages:
  1863.                 pkg.read_version_info()
  1864.                 pkg.set_default_runtime_from_version_info()
  1865.                 if pkg.shared_files or pkg.private_files:
  1866.                     pkg.update_bytecode_files(runtimes, default_rt, bc_option)
  1867.         except PyCentralError, msg:
  1868.             self.error(msg)
  1869.  
  1870. register_action(ActionUpdateDefault)
  1871.  
  1872.  
  1873. class ActionList(Action):
  1874.     name = 'list'
  1875.     help = 'List all pycentral-managed files'
  1876.  
  1877.     def check_args(self, global_options):
  1878.         if len(self.args) != 0:
  1879.             self._option_parser.print_help()
  1880.             sys.exit(1)
  1881.         return self.errors_occured
  1882.  
  1883.     def run(self, global_options):
  1884.         runtimes = get_installed_runtimes(with_unsupported=True)
  1885.     for (p, v) in read_dpkg_status():
  1886.         pkg = DebPackage('package', p)
  1887.         pkg.read_version_info()
  1888.         for f in pkg.list(runtimes, list_script_files=True):
  1889.         print f
  1890.  
  1891. register_action(ActionList)
  1892.  
  1893.  
  1894. class ActionShowDefault(Action):
  1895.     name = 'showdefault'
  1896.     help = 'Show default python version number'
  1897.  
  1898.     def check_args(self, global_options):
  1899.         if len(self.args) != 0:
  1900.             self._option_parser.print_help()
  1901.             sys.exit(1)
  1902.         return self.errors_occured
  1903.  
  1904.     def run(self, global_options):
  1905.         print pyversions.default_version(version_only=True)
  1906.         sys.stderr.write("pycentral showdefault is deprecated, use `pyversions -vd'\n")
  1907.  
  1908. register_action(ActionShowDefault)
  1909.  
  1910.  
  1911. class ActionShowVersions(Action):
  1912.     name = 'showversions'
  1913.     help = 'Show version numbers of supported python versions'
  1914.  
  1915.     def check_args(self, global_options):
  1916.         if len(self.args) != 0:
  1917.             self._option_parser.print_help()
  1918.             sys.exit(1)
  1919.  
  1920.         return self.errors_occured
  1921.  
  1922.     def run(self, global_options):
  1923.         supported = pyversions.supported_versions()
  1924.         versions = [d[6:] for d in supported if re.match(r'python\d\.\d', d)]
  1925.         print ' '.join(versions)
  1926.         sys.stderr.write("pycentral showversions is deprecated, use `pyversions -vs'\n")
  1927.  
  1928. register_action(ActionShowVersions)
  1929.  
  1930. class ActionShowSupported(Action):
  1931.     name = 'showsupported'
  1932.     help = 'Show the supported python versions'
  1933.  
  1934.     def check_args(self, global_options):
  1935.         if len(self.args) != 0:
  1936.             self._option_parser.print_help()
  1937.             sys.exit(1)
  1938.         return self.errors_occured
  1939.  
  1940.     def run(self, global_options):
  1941.         supported = pyversions.supported_versions()
  1942.         print ' '.join(supported)
  1943.         sys.stderr.write("pycentral showsupported is deprecated, use `pyversions -s'\n")
  1944.  
  1945. register_action(ActionShowSupported)
  1946.  
  1947.  
  1948. class ActionPyCentralDir(Action):
  1949.     name = 'pycentraldir'
  1950.     help = 'Show the pycentral installation directory for the package'
  1951.     usage = '<package>'
  1952.  
  1953.     def check_args(self, global_options):
  1954.         if len(self.args) != 1:
  1955.             self._option_parser.print_help()
  1956.             sys.exit(1)
  1957.         self.pkgname = self.args[0]
  1958.         return self.errors_occured
  1959.  
  1960.     def run(self, global_options):
  1961.         if shared_base2[-1] == '/':
  1962.             print shared_base2[:-1]
  1963.         else:
  1964.             print shared_base2
  1965.  
  1966. register_action(ActionPyCentralDir)
  1967.  
  1968.  
  1969. class ActionVersion(Action):
  1970.     name = 'version'
  1971.     help = 'Show the pycentral version'
  1972.  
  1973.     def check_args(self, global_options):
  1974.         if len(self.args) != 0:
  1975.             self._option_parser.print_help()
  1976.             sys.exit(1)
  1977.  
  1978.         return self.errors_occured
  1979.  
  1980.     def run(self, global_options):
  1981.         sys.stdout.write("%s\n" % pycentral_version)
  1982.  
  1983. register_action(ActionVersion)
  1984.  
  1985.  
  1986. class ActionDebhelper(Action):
  1987.     name = 'debhelper'
  1988.     help = 'move files to pycentral location, variable substitutions'
  1989.     usage = '[-p|--provides] [--no-move|--include-links] <package> [<package directory>]'
  1990.  
  1991.     def get_option_parser(self):
  1992.         if not self._option_parser:
  1993.             envvar = os.environ.get('DH_PYCENTRAL', '')
  1994.             substvars_default = 'no'
  1995.             if 'substvars=file' in envvar:
  1996.                 substvars_default = 'file'
  1997.             if 'substvars=stdout' in envvar:
  1998.                 substvars_default = 'stdout'
  1999.  
  2000.             p = OptionParser()
  2001.             p.add_option('-p', '--provides',
  2002.                          help="generate substitution for python:Provides",
  2003.                          default='add-provides' in envvar, action='store_true', dest='provides')
  2004.             p.add_option('--no-move', '--nomove',
  2005.                          help="do not move files to pycentral location",
  2006.                          default='no-move' in envvar or 'nomove' in envvar, action='store_true', dest='nomove')
  2007.             p.add_option('--include-links',
  2008.                          help="include links to the pycentral location",
  2009.                          default='include-links' in envvar, action='store_true', dest='includelinks')
  2010.             p.add_option('--stdout',
  2011.                          help="just print substitution variables to stdout",
  2012.                          default='stdout' in envvar, action='store_true', dest='stdout')
  2013.             p.add_option('--substvars',
  2014.                          help="where to print substitution vars (no, file, stdout)",
  2015.                          default=substvars_default, dest='substvars')
  2016.             p.add_option('--no-act', '--dry-run',
  2017.                          help="dry run",
  2018.                          default=('dry-run' in envvar) or ('no-act' in envvar),
  2019.                          action='store_true', dest='dryrun')
  2020.             self._option_parser = p
  2021.         return self._option_parser
  2022.  
  2023.     def check_args(self, global_options):
  2024.         if not len(self.args) in (1, 2):
  2025.             self._option_parser.print_help()
  2026.             sys.exit(1)
  2027.         if 'file' in self.options.substvars:
  2028.             self.options.substvars = 'file'
  2029.         if 'stdout' in self.options.substvars:
  2030.             self.options.substvars = 'stdout'
  2031.         if self.options.nomove and self.options.includelinks:
  2032.             self.error("options `--no-move' and `--include-links' conflict")
  2033.         return self.errors_occured
  2034.  
  2035.     def run(self, global_options):
  2036.         if len(self.args) < 2:
  2037.             pkgdir = 'debian/' + self.args[0]
  2038.         else:
  2039.             pkgdir = self.args[1]
  2040.         try:
  2041.             pkg = DebPackage('package', self.args[0], pkgdir=pkgdir,
  2042.                              parse_versions=self.options.substvars!='no')
  2043.             if not self.options.nomove:
  2044.                 pkg.move_files()
  2045.             if self.options.includelinks:
  2046.                 pkg.add_shared_links()
  2047.             pkg.read_pyfiles()
  2048.             if self.options.substvars!='no':
  2049.                 pkg.gen_substvars()
  2050.         except PyCentralVersionMissingError, msg:
  2051.             self.warn(msg)
  2052.             return
  2053.         except PyCentralError, msg:
  2054.             self.error(msg)
  2055.  
  2056.         out = None
  2057.         if self.options.stdout or self.options.substvars == 'stdout':
  2058.             out = sys.stdout
  2059.         elif self.options.substvars == 'file':
  2060.             out = file('debian/%s.substvars' % pkg.name, 'a+')
  2061.         if out:
  2062.             out.write('python:Versions=%s\n' % pkg.version_field)
  2063.             out.write('python:Depends=%s\n' % pkg.depends)
  2064.             out.write('python:Provides=%s\n' % pkg.provides)
  2065.  
  2066.  
  2067. register_action(ActionDebhelper)
  2068.  
  2069. # match a string with the list of available actions
  2070. def action_matches(action, actions):
  2071.     prog = re.compile('[^-]*?-'.join(action.split('-')))
  2072.     return [a for a in actions if prog.match(a)]
  2073.  
  2074. def usage(stream, msg=None):
  2075.     print >>stream, msg
  2076.     print >>stream, "use `%s help' for help on actions and arguments" % program
  2077.     print >>stream
  2078.     sys.exit(1)
  2079.  
  2080. # parse command line arguments
  2081. def parse_options(args):
  2082.     shortusage = 'usage: %s [<option> ...] <action> <pkgname>' % program
  2083.     parser = OptionParser(usage=shortusage)
  2084.     parser.disable_interspersed_args()
  2085.  
  2086.     # setup the parsers object
  2087.     parser.remove_option('-h')
  2088.     parser.add_option('-h', '--help',
  2089.                       help='help screen',
  2090.                       action='store_true', dest='help')
  2091.     parser.add_option('-v', '--verbose',
  2092.                       help='verbose mode',
  2093.                       action='store_true', dest='verbose')
  2094.  
  2095.     global_options, args = parser.parse_args()
  2096.     # Print the help screen and exit
  2097.     if len(args) == 0 or global_options.help:
  2098.         parser.print_help()
  2099.         print "\nactions:"
  2100.         action_names = known_actions.keys()
  2101.         action_names.sort()
  2102.         for n in action_names:
  2103.             print "  %-21s %s" % (n, known_actions[n].help)
  2104.         print ""
  2105.         sys.exit(1)
  2106.  
  2107.     # check if the specified action really exists
  2108.     action_name = args[0]
  2109.     del args[0]
  2110.     matching_actions = action_matches(action_name, known_actions.keys())
  2111.     if len(matching_actions) == 0:
  2112.         usage(sys.stderr, "unknown action `%s'" % action_name)
  2113.     elif len(matching_actions) > 1:
  2114.         usage(sys.stderr,
  2115.               "ambiguous action `%s', matching actions: %s"
  2116.               % (action_name, strlist(matching_actions)))
  2117.     else:
  2118.         action_name = matching_actions[0]
  2119.  
  2120.     # instantiate an object for the action and parse the remaining arguments
  2121.     action = known_actions[action_name]()
  2122.     action_options, action_names = action.parse_args(args)
  2123.  
  2124.     return global_options, action
  2125.  
  2126. class Logging:
  2127.     DEBUG, INFO, WARN, ERROR = range(4)
  2128.     def __init__(self, level=WARN):
  2129.         self.fd = None
  2130.         self.level = level
  2131.         try:
  2132.             self.fd = file('/var/log/pycentral.log', 'a+')
  2133.         except IOError:
  2134.             self.fd = None
  2135.     def msg(self, level, s):
  2136.         if level < self.level:
  2137.             return
  2138.         d = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2139.         if self.fd:
  2140.             self.fd.write('%s %s %s\n' % (d, level, s))
  2141.             self.fd.flush()
  2142.         sys.stdout.write('pycentral: %s\n' % (s))
  2143.         sys.stdout.flush()
  2144.     def info(self, s):
  2145.         self.msg(self.INFO, s)
  2146.     def warn(self, s):
  2147.         self.msg(self.WARN, s)
  2148.     def error(self, s):
  2149.         self.msg(self.ERROR, s)
  2150.         sys.stderr.write('%s\n' % s)
  2151.     def debug(self, s):
  2152.         self.msg(self.DEBUG, s)
  2153.  
  2154.     def debug_list(self, tab, s, l, n=4):
  2155.         l2 = l[:min(n, len(l))]
  2156.         if len(l) > n:
  2157.             l2.append('...')
  2158.         self.msg(self.DEBUG, "%s%s (%s/%s)" % (tab, s, len(l2), len(l)))
  2159.         if len(l2) > 0:
  2160.             logging.debug('%s    %s' % (tab, l2))
  2161.  
  2162. def setup_logging(loglevel=Logging.WARN, verbose=False):
  2163.     levels = ['debug', 'info', 'warn', 'error']
  2164.     env_level = os.environ.get('PYCENTRAL', 'warn').lower()
  2165.     for i in range(len(levels)):
  2166.         if env_level.find(levels[i]) != -1:
  2167.             loglevel = i
  2168.     if verbose:
  2169.         loglevel = Logging.DEBUG
  2170.     global logging
  2171.     logging = Logging(loglevel)
  2172.  
  2173. def main():
  2174.     global_options, action = parse_options(sys.argv[1:])
  2175.  
  2176.     os.umask(0022)
  2177.  
  2178.     # setup logging stuff
  2179.     setup_logging(Logging.WARN, global_options.verbose)
  2180.     if action.name == 'debhelper' or action.name.startswith('show'):
  2181.         pass
  2182.     else:
  2183.         logging.debug('pycentral ' + ' '.join(sys.argv[1:]))
  2184.  
  2185.     # check the arguments according to the action called
  2186.     if action.check_args(global_options):
  2187.         sys.exit(1)
  2188.  
  2189.     # run the action and exit
  2190.     rv = action.run(global_options)
  2191.     sys.exit(rv)
  2192.  
  2193.  
  2194. # call the main routine
  2195. if __name__ == '__main__':
  2196.     main()
  2197.